package com.ops.service.ns.system.beans;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

import com.ops.schema.ns.system.OPSLookUps;
import com.ops.schema.ns.system.OPSRecordAuditInfo;
import com.ops.schema.ns.system.SearchLookUpsRequest;
import com.ops.schema.ns.system.SearchLookUpsResponse;
import com.ops.schema.ns.system.UpdateLookUpsRequest;
import com.ops.schema.ns.system.UpdateLookUpsResponse;
import com.ops.service.ns.system.client.OnlinePayrollSystemServiceClient;
import com.ops.service.ns.system.exception.ServiceException;

public class LookUpMBean {

	private String lookUpId;
	private String lookUpType;
	private String activeFrom;
	private String activeTo;
	private String mode;
	private String delflag;
	private String saveStatus;
	private List<OPSLookUps> opsLookUplst;
	private OPSLookUps opsLookUp;
	private Map<String, OPSLookUps> opslkUpMap;

	public void searchLookUp() {
		System.out.println("Inside searchLookUps");
		SearchLookUpsRequest lkUpRequest = new SearchLookUpsRequest();
		try {
			lkUpRequest.setName(lookUpId);
			lkUpRequest.setType(lookUpType);
			lkUpRequest.setActiveFrom(activeFrom);
			lkUpRequest.setActiveTo(activeTo);
			OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
			uditInfo.setUpdatedBy(getuserId());
			uditInfo.setUpdatedSession(getSessionId());
			uditInfo.setAction("S");
			uditInfo.setObjectType(getMenuId());
			lkUpRequest.setOPSRecordAuditInfo(uditInfo);
			SearchLookUpsResponse lookUpsResponse = OnlinePayrollSystemServiceClient
					.searchLookUp(lkUpRequest);
			setOpsLookUplst(lookUpsResponse.getOPSLookUps());
			for(OPSLookUps lkUps:lookUpsResponse.getOPSLookUps())
			{
				System.out.println("##"+lkUps.getActiveFrom()+":::"+lkUps.getActiveTo()+":::"+lkUps.getAssociatedRuleName());
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}

	public void saveLookUps(ActionEvent event) {

		UpdateLookUpsRequest updateRequest = new UpdateLookUpsRequest();
		OPSLookUps opsLookUps = new OPSLookUps();
		opsLookUps.setType(opsLookUp.getType());
		opsLookUps.setName(opsLookUp.getName());
		opsLookUps.setDescription(opsLookUp.getDescription());
		opsLookUps.setId(opsLookUp.getId());
		opsLookUps.setCode(opsLookUp.getCode());
		opsLookUps.setActiveFrom(opsLookUp.getActiveFrom());
		opsLookUps.setActiveTo(opsLookUp.getActiveTo());
		opsLookUps.setAssociatedRuleId(opsLookUp.getAssociatedRuleId());
		opsLookUps.setOperationFlag("U");
		opsLookUps.setAssociatedRuleName(opsLookUp.getAssociatedRuleName());
		
		
		OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
		uditInfo.setUpdatedBy(getuserId());
		uditInfo.setUpdatedSession(getSessionId());
		opsLookUps.setOPSRecordAuditInfo(uditInfo);
		if(opsLookUps.getId() != null)
		uditInfo.setAction("U");
		else
		uditInfo.setAction("I");	
		uditInfo.setObjectType(getMenuId());
		updateRequest.getOPSLookUps().add(opsLookUps);
		try {
			UpdateLookUpsResponse response=persistData(updateRequest);
			setSaveStatus(response.getServiceResult().getStatusMsg());
			searchLookUp();
		} catch (ServiceException e) {
			e.printStackTrace();
		}

	}

	public void resetLookUps() {
		setLookUpId(null);
		setLookUpType(null);
		setActiveFrom(null);
		setActiveTo(null);
	}
	
	public void delLookUps(ActionEvent event) {
		if (mode != null && !mode.equalsIgnoreCase("")) {
			if (mode.startsWith("Del_") && delflag != null
					&& delflag.equalsIgnoreCase("Yes")) {
				setDelflag(null);
				UpdateLookUpsRequest updateRequest = new UpdateLookUpsRequest();
				String key = mode.replace("Del_", "").trim();
				OPSLookUps lookUps = new OPSLookUps();
				lookUps.setId(key);
				lookUps.setOperationFlag("D");
				OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
				uditInfo.setUpdatedBy(getuserId());
				uditInfo.setUpdatedSession(getSessionId());
				uditInfo.setAction("D");	
				uditInfo.setObjectType(getMenuId());
				lookUps.setOPSRecordAuditInfo(uditInfo);
				updateRequest.getOPSLookUps().add(lookUps);
				try {
					persistData(updateRequest);
					searchLookUp();
				} catch (ServiceException e) {
					e.printStackTrace();
				}

			}
		}
	}

	public OPSLookUps addLookUps() {
		setSaveStatus(null);
		if (mode != null && !mode.equalsIgnoreCase("")) {
			if (getOpsLookUplst() != null) {
				opslkUpMap = new HashMap<String, OPSLookUps>();
				for (OPSLookUps LookUps : getOpsLookUplst()) {
					System.out.println("Inside" + LookUps.getId());
					opslkUpMap.put(LookUps.getId(), LookUps);
				}
			}
			if (mode.equalsIgnoreCase("newAdd")) {
				opsLookUp = new OPSLookUps();
			} else if (mode.startsWith("Edit_")) {
				String key = mode.replace("Edit_", "").trim();
				System.out.println("Key:" + key);
				opsLookUp = opslkUpMap.get(key);
			}
		}
		System.out.println("Inside addLookUps:::" + mode);
		return opsLookUp;
	}

	
	public UpdateLookUpsResponse persistData(
			UpdateLookUpsRequest lookUpsRequest) throws ServiceException {
		UpdateLookUpsResponse lookUpsResponse = OnlinePayrollSystemServiceClient.saveLookUp(lookUpsRequest);			
		return lookUpsResponse;
	}

	private String getSessionId() {

		FacesContext fCtx = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(false);
		return session.getId();
	}
	private String getuserId() {

		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		return sessionMap.get("UserId").toString();
	}
	private String getMenuId() {

		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		return sessionMap.get("MenuId").toString();
	}
	
	
	public String getLookUpId() {
		return lookUpId;
	}

	public void setLookUpId(String lookUpId) {
		this.lookUpId = lookUpId;
	}

	public String getLookUpType() {
		return lookUpType;
	}

	public void setLookUpType(String lookUpType) {
		this.lookUpType = lookUpType;
	}

	public String getActiveFrom() {
		return activeFrom;
	}

	public void setActiveFrom(String activeFrom) {
		this.activeFrom = activeFrom;
	}

	public String getActiveTo() {
		return activeTo;
	}

	public void setActiveTo(String activeTo) {
		this.activeTo = activeTo;
	}

	public String getMode() {
		return mode;
	}

	public void setMode(String mode) {
		this.mode = mode;
	}

	public List<OPSLookUps> getOpsLookUplst() {
		return opsLookUplst;
	}

	public void setOpsLookUplst(List<OPSLookUps> opsLookUplst) {
		this.opsLookUplst = opsLookUplst;
	}

	public OPSLookUps getOpsLookUp() {
		return opsLookUp;
	}

	public void setOpsLookUp(OPSLookUps opsLookUp) {
		this.opsLookUp = opsLookUp;
	}

	public Map<String, OPSLookUps> getOpslkUpMap() {
		return opslkUpMap;
	}

	public void setOpslkUpMap(Map<String, OPSLookUps> opslkUpMap) {
		this.opslkUpMap = opslkUpMap;
	}

	public String getDelflag() {
		return delflag;
	}

	public void setDelflag(String delflag) {
		this.delflag = delflag;
	}

	public String getSaveStatus() {
		return saveStatus;
	}

	public void setSaveStatus(String saveStatus) {
		this.saveStatus = saveStatus;
	}


}
